home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Monster Media 1996 #15
/
Monster Media Number 15 (Monster Media)(July 1996).ISO
/
prog_c
/
cuj0696.zip
/
DWYER.ZIP
/
SPECTRAL.TST
/
GETGCD.CX
< prev
next >
Wrap
Text File
|
1996-03-09
|
2KB
|
78 lines
/* ============ */
/* getgcd.cx */
/* ============ */
#include <xtendefs.h>
#include <stdio.h>
/* ==================================================================== */
/* GetGCD - finds greatest common divisor by Euclid's algorithn */
/* ==================================================================== */
void
GetGCD(USHORT *a, USHORT *b, USHORT *m)
{
USHORT n[NE], t[NE];
XCOPY(a, m); XABS(m); XFLOOR(m, m);
XCOPY(b, n); XABS(n); XFLOOR(n, n);
if (XCMP(m, n) == -1)
{
XSWAP(m, n)
}
while (XTST(n) > 0)
{
XMOD(m, n, t); /* t = mod(m,n) */
XCOPY(n, m); /* m = n */
XCOPY(t, n); /* n = t */
}
}
#define TEST
# undef TEST
# ifdef TEST
#define MAIN main
#define GETGCD GetGCD
#define GETSTR GetStr
#define FLUSH_LINE(u) \
{ \
int x; \
do \
{ \
x = getc(u); \
} \
while (x != EOF && x != '\n'); \
}
/* ==================================================== */
/* GetStr - prompts operator with s to get string Str */
/* ==================================================== */
void
GETSTR(char *s, char *Str)
{
fprintf(stderr, "%s", s);
scanf("%s", Str);
FLUSH_LINE(stdin);
}
void
MAIN()
{
char u[128], v[128];
USHORT m[NE], n[NE], r[NE];
puts("Testing Euclid's algorithm: Needs two integers per call\n");
while (main)
{
GETSTR("Enter first of the next pair: ", u);
GETSTR("Enter the second: ", v);
ASCTOX(u, m);
ASCTOX(v, n);
GETGCD(m, n, r);
# if 0
printf("GCD = %.Lf\n", GETGCD(u,v));
# endif
XPRINTF("GCD = ", r, 10);
}
}
# endif